{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导入第三方包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-15T00:52:16.629173Z",
     "start_time": "2021-03-15T00:52:16.621194Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import gc\n",
    "import math\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import lightgbm as lgb\n",
    "import xgboost as xgb\n",
    "from catboost import CatBoostRegressor\n",
    "from sklearn.linear_model import SGDRegressor, LinearRegression, Ridge\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "\n",
    "\n",
    "from sklearn.model_selection import StratifiedKFold, KFold\n",
    "from sklearn.metrics import log_loss\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "\n",
    "from tqdm import tqdm\n",
    "import matplotlib.pyplot as plt\n",
    "import time\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-15T00:52:22.085956Z",
     "start_time": "2021-03-15T00:52:19.571864Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>heartbeat_signals</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.9912297987616655,0.9435330436439665,0.764677...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0.9714822034884503,0.9289687459588268,0.572932...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1.0,0.9591487564065292,0.7013782792997189,0.23...</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>0.9757952826275774,0.9340884687738161,0.659636...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>0.0,0.055816398940721094,0.26129357194994196,0...</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id                                  heartbeat_signals  label\n",
       "0   0  0.9912297987616655,0.9435330436439665,0.764677...    0.0\n",
       "1   1  0.9714822034884503,0.9289687459588268,0.572932...    0.0\n",
       "2   2  1.0,0.9591487564065292,0.7013782792997189,0.23...    2.0\n",
       "3   3  0.9757952826275774,0.9340884687738161,0.659636...    0.0\n",
       "4   4  0.0,0.055816398940721094,0.26129357194994196,0...    2.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv('./data/train.csv')\n",
    "test=pd.read_csv('./data/testA.csv')\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-15T00:52:41.773931Z",
     "start_time": "2021-03-15T00:52:41.760966Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>heartbeat_signals</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>100000</td>\n",
       "      <td>0.9915713654170097,1.0,0.6318163407681274,0.13...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>100001</td>\n",
       "      <td>0.6075533139615096,0.5417083883163654,0.340694...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>100002</td>\n",
       "      <td>0.9752726292239277,0.6710965234906665,0.686758...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>100003</td>\n",
       "      <td>0.9956348033996116,0.9170249621481004,0.521096...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>100004</td>\n",
       "      <td>1.0,0.8879490481178918,0.745564725322326,0.531...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       id                                  heartbeat_signals\n",
       "0  100000  0.9915713654170097,1.0,0.6318163407681274,0.13...\n",
       "1  100001  0.6075533139615096,0.5417083883163654,0.340694...\n",
       "2  100002  0.9752726292239277,0.6710965234906665,0.686758...\n",
       "3  100003  0.9956348033996116,0.9170249621481004,0.521096...\n",
       "4  100004  1.0,0.8879490481178918,0.745564725322326,0.531..."
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-15T00:53:20.837171Z",
     "start_time": "2021-03-15T00:53:20.824203Z"
    }
   },
   "outputs": [],
   "source": [
    "def reduce_mem_usage(df):\n",
    "    start_mem = df.memory_usage().sum() / 1024**2 \n",
    "    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n",
    "    \n",
    "    for col in df.columns:\n",
    "        col_type = df[col].dtype\n",
    "        \n",
    "        if col_type != object:\n",
    "            c_min = df[col].min()\n",
    "            c_max = df[col].max()\n",
    "            if str(col_type)[:3] == 'int':\n",
    "                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n",
    "                    df[col] = df[col].astype(np.int8)\n",
    "                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n",
    "                    df[col] = df[col].astype(np.int16)\n",
    "                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n",
    "                    df[col] = df[col].astype(np.int32)\n",
    "                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n",
    "                    df[col] = df[col].astype(np.int64)  \n",
    "            else:\n",
    "                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n",
    "                    df[col] = df[col].astype(np.float16)\n",
    "                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n",
    "                    df[col] = df[col].astype(np.float32)\n",
    "                else:\n",
    "                    df[col] = df[col].astype(np.float64)\n",
    "        else:\n",
    "            df[col] = df[col].astype('category')\n",
    "\n",
    "    end_mem = df.memory_usage().sum() / 1024**2 \n",
    "    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n",
    "    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n",
    "    \n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0,\n",
       "       '0.9912297987616655,0.9435330436439665,0.7646772997256593,0.6185708990212999,0.3796321642826237,0.19082233510621885,0.040237131594430715,0.02599520771717858,0.03170886048677242,0.06552357497104398,0.12553088566683082,0.14674736762087337,0.16765635354203254,0.19337353075154495,0.22613482558418235,0.2211427948707646,0.23606736350657742,0.2211427948707646,0.2211427948707646,0.21110661221417562,0.20858662883955462,0.19337353075154495,0.19592021355822875,0.1984624088145674,0.18570638844539308,0.19592020417425474,0.18314160533045887,0.19337353075154495,0.19082233510621885,0.20858662883955462,0.2211427948707646,0.2508391000672623,0.2606035735248363,0.27753397418529446,0.2942679945470305,0.3037438606924122,0.3364276621747203,0.3479233126336631,0.38410562561692113,0.3863371817756788,0.4084648300418338,0.4106590521686313,0.42592580507675887,0.42592580507675887,0.4291763526701312,0.4324195928902589,0.42809365036122277,0.42809365036122277,0.4128499421288813,0.41503751002775946,0.39744344526503395,0.40186210145863127,0.38856529150062463,0.4040663811296295,0.3952290154247498,0.39854937055983863,0.40186210145863127,0.41722173372658117,0.43457767776945677,0.4302582430505069,0.4324195928902589,0.41503751002775946,0.43457767776945677,0.42592580507675887,0.39744344526503395,0.3570543268368737,0.3456314920490284,0.334117492319521,0.3178419565867778,0.3201783146071371,0.3178419565867778,0.334117492319521,0.3248396760909577,0.334117492319521,0.3248396760909577,0.3364276621747203,0.32716473826850506,0.331803634364786,0.3364276621747203,0.32716473826850506,0.334117492319521,0.334117492319521,0.3456314920490284,0.34333602495581456,0.35477697711039125,0.334117492319521,0.334117492319521,0.4062672980405093,0.4957985125670452,0.5800967406106492,0.6925715000677044,0.8284346202194008,0.9465607483396898,1.0,0.9779737148370964,0.8528463111218912,0.6925715000677044,0.518357117251287,0.26546113853132874,0.18057224500953173,0.09037444062714899,0.0,0.011611290892655711,0.014499569681678932,0.07662128178823865,0.11749356439958623,0.15462379539892293,0.18314160533045887,0.21613343067782795,0.20858662883955462,0.21236496446001718,0.21613343067782795,0.20858662883955462,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0',\n",
       "       0.0], dtype=object)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.values[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-15T00:54:18.244721Z",
     "start_time": "2021-03-15T00:53:59.807775Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Memory usage of dataframe is 157.93 MB\n",
      "Memory usage after optimization is: 39.67 MB\n",
      "Decreased by 74.9%\n",
      "Memory usage of dataframe is 31.43 MB\n",
      "Memory usage after optimization is: 7.90 MB\n",
      "Decreased by 74.9%\n"
     ]
    }
   ],
   "source": [
    "# 简单预处理\n",
    "train_list = []\n",
    "\n",
    "for items in train.values:\n",
    "    train_list.append([items[0]] + [float(i) for i in items[1].split(',')] + [items[2]])  # 将每一个items处理成列表形式\n",
    "\n",
    "train = pd.DataFrame(np.array(train_list))\n",
    "train.columns = ['id'] + ['s_'+str(i) for i in range(len(train_list[0])-2)] + ['label']  # 处理feature的名字\n",
    "train = reduce_mem_usage(train)  # 从数据结构方面优化存储\n",
    "\n",
    "test_list=[]\n",
    "for items in test.values:\n",
    "    test_list.append([items[0]] + [float(i) for i in items[1].split(',')])\n",
    "\n",
    "test = pd.DataFrame(np.array(test_list))\n",
    "test.columns = ['id'] + ['s_'+str(i) for i in range(len(test_list[0])-1)]\n",
    "test = reduce_mem_usage(test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-15T00:54:57.351196Z",
     "start_time": "2021-03-15T00:54:57.321310Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>s_0</th>\n",
       "      <th>s_1</th>\n",
       "      <th>s_2</th>\n",
       "      <th>s_3</th>\n",
       "      <th>s_4</th>\n",
       "      <th>s_5</th>\n",
       "      <th>s_6</th>\n",
       "      <th>s_7</th>\n",
       "      <th>s_8</th>\n",
       "      <th>...</th>\n",
       "      <th>s_196</th>\n",
       "      <th>s_197</th>\n",
       "      <th>s_198</th>\n",
       "      <th>s_199</th>\n",
       "      <th>s_200</th>\n",
       "      <th>s_201</th>\n",
       "      <th>s_202</th>\n",
       "      <th>s_203</th>\n",
       "      <th>s_204</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.991211</td>\n",
       "      <td>0.943359</td>\n",
       "      <td>0.764648</td>\n",
       "      <td>0.618652</td>\n",
       "      <td>0.379639</td>\n",
       "      <td>0.190796</td>\n",
       "      <td>0.040222</td>\n",
       "      <td>0.026001</td>\n",
       "      <td>0.031708</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.971680</td>\n",
       "      <td>0.929199</td>\n",
       "      <td>0.572754</td>\n",
       "      <td>0.178467</td>\n",
       "      <td>0.122986</td>\n",
       "      <td>0.132324</td>\n",
       "      <td>0.094421</td>\n",
       "      <td>0.089600</td>\n",
       "      <td>0.030487</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.958984</td>\n",
       "      <td>0.701172</td>\n",
       "      <td>0.231812</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.080688</td>\n",
       "      <td>0.128418</td>\n",
       "      <td>0.187500</td>\n",
       "      <td>0.280762</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.0</td>\n",
       "      <td>0.975586</td>\n",
       "      <td>0.934082</td>\n",
       "      <td>0.659668</td>\n",
       "      <td>0.249878</td>\n",
       "      <td>0.237061</td>\n",
       "      <td>0.281494</td>\n",
       "      <td>0.249878</td>\n",
       "      <td>0.249878</td>\n",
       "      <td>0.241455</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.055817</td>\n",
       "      <td>0.261230</td>\n",
       "      <td>0.359863</td>\n",
       "      <td>0.433105</td>\n",
       "      <td>0.453613</td>\n",
       "      <td>0.499023</td>\n",
       "      <td>0.542969</td>\n",
       "      <td>0.616699</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 207 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    id       s_0       s_1       s_2       s_3       s_4       s_5       s_6  \\\n",
       "0  0.0  0.991211  0.943359  0.764648  0.618652  0.379639  0.190796  0.040222   \n",
       "1  1.0  0.971680  0.929199  0.572754  0.178467  0.122986  0.132324  0.094421   \n",
       "2  2.0  1.000000  0.958984  0.701172  0.231812  0.000000  0.080688  0.128418   \n",
       "3  3.0  0.975586  0.934082  0.659668  0.249878  0.237061  0.281494  0.249878   \n",
       "4  4.0  0.000000  0.055817  0.261230  0.359863  0.433105  0.453613  0.499023   \n",
       "\n",
       "        s_7       s_8  ...  s_196  s_197  s_198  s_199  s_200  s_201  s_202  \\\n",
       "0  0.026001  0.031708  ...    0.0    0.0    0.0    0.0    0.0    0.0    0.0   \n",
       "1  0.089600  0.030487  ...    0.0    0.0    0.0    0.0    0.0    0.0    0.0   \n",
       "2  0.187500  0.280762  ...    0.0    0.0    0.0    0.0    0.0    0.0    0.0   \n",
       "3  0.249878  0.241455  ...    0.0    0.0    0.0    0.0    0.0    0.0    0.0   \n",
       "4  0.542969  0.616699  ...    0.0    0.0    0.0    0.0    0.0    0.0    0.0   \n",
       "\n",
       "   s_203  s_204  label  \n",
       "0    0.0    0.0    0.0  \n",
       "1    0.0    0.0    0.0  \n",
       "2    0.0    0.0    2.0  \n",
       "3    0.0    0.0    0.0  \n",
       "4    0.0    0.0    2.0  \n",
       "\n",
       "[5 rows x 207 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-15T00:55:36.644040Z",
     "start_time": "2021-03-15T00:55:36.619678Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>s_0</th>\n",
       "      <th>s_1</th>\n",
       "      <th>s_2</th>\n",
       "      <th>s_3</th>\n",
       "      <th>s_4</th>\n",
       "      <th>s_5</th>\n",
       "      <th>s_6</th>\n",
       "      <th>s_7</th>\n",
       "      <th>s_8</th>\n",
       "      <th>...</th>\n",
       "      <th>s_195</th>\n",
       "      <th>s_196</th>\n",
       "      <th>s_197</th>\n",
       "      <th>s_198</th>\n",
       "      <th>s_199</th>\n",
       "      <th>s_200</th>\n",
       "      <th>s_201</th>\n",
       "      <th>s_202</th>\n",
       "      <th>s_203</th>\n",
       "      <th>s_204</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>100000.0</td>\n",
       "      <td>0.991699</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.631836</td>\n",
       "      <td>0.136230</td>\n",
       "      <td>0.041412</td>\n",
       "      <td>0.102722</td>\n",
       "      <td>0.120850</td>\n",
       "      <td>0.123413</td>\n",
       "      <td>0.107910</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>100001.0</td>\n",
       "      <td>0.607422</td>\n",
       "      <td>0.541504</td>\n",
       "      <td>0.340576</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.090698</td>\n",
       "      <td>0.164917</td>\n",
       "      <td>0.195068</td>\n",
       "      <td>0.168823</td>\n",
       "      <td>0.198853</td>\n",
       "      <td>...</td>\n",
       "      <td>0.389893</td>\n",
       "      <td>0.386963</td>\n",
       "      <td>0.367188</td>\n",
       "      <td>0.364014</td>\n",
       "      <td>0.360596</td>\n",
       "      <td>0.357178</td>\n",
       "      <td>0.350586</td>\n",
       "      <td>0.350586</td>\n",
       "      <td>0.350586</td>\n",
       "      <td>0.36377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>100002.0</td>\n",
       "      <td>0.975098</td>\n",
       "      <td>0.670898</td>\n",
       "      <td>0.686523</td>\n",
       "      <td>0.708496</td>\n",
       "      <td>0.718750</td>\n",
       "      <td>0.716797</td>\n",
       "      <td>0.720703</td>\n",
       "      <td>0.701660</td>\n",
       "      <td>0.596680</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>100003.0</td>\n",
       "      <td>0.995605</td>\n",
       "      <td>0.916992</td>\n",
       "      <td>0.520996</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.221802</td>\n",
       "      <td>0.404053</td>\n",
       "      <td>0.490479</td>\n",
       "      <td>0.527344</td>\n",
       "      <td>0.518066</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>100004.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.888184</td>\n",
       "      <td>0.745605</td>\n",
       "      <td>0.531738</td>\n",
       "      <td>0.380371</td>\n",
       "      <td>0.224609</td>\n",
       "      <td>0.091125</td>\n",
       "      <td>0.057648</td>\n",
       "      <td>0.003914</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.00000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 206 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         id       s_0       s_1       s_2       s_3       s_4       s_5  \\\n",
       "0  100000.0  0.991699  1.000000  0.631836  0.136230  0.041412  0.102722   \n",
       "1  100001.0  0.607422  0.541504  0.340576  0.000000  0.090698  0.164917   \n",
       "2  100002.0  0.975098  0.670898  0.686523  0.708496  0.718750  0.716797   \n",
       "3  100003.0  0.995605  0.916992  0.520996  0.000000  0.221802  0.404053   \n",
       "4  100004.0  1.000000  0.888184  0.745605  0.531738  0.380371  0.224609   \n",
       "\n",
       "        s_6       s_7       s_8  ...     s_195     s_196     s_197     s_198  \\\n",
       "0  0.120850  0.123413  0.107910  ...  0.000000  0.000000  0.000000  0.000000   \n",
       "1  0.195068  0.168823  0.198853  ...  0.389893  0.386963  0.367188  0.364014   \n",
       "2  0.720703  0.701660  0.596680  ...  0.000000  0.000000  0.000000  0.000000   \n",
       "3  0.490479  0.527344  0.518066  ...  0.000000  0.000000  0.000000  0.000000   \n",
       "4  0.091125  0.057648  0.003914  ...  0.000000  0.000000  0.000000  0.000000   \n",
       "\n",
       "      s_199     s_200     s_201     s_202     s_203    s_204  \n",
       "0  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  \n",
       "1  0.360596  0.357178  0.350586  0.350586  0.350586  0.36377  \n",
       "2  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  \n",
       "3  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  \n",
       "4  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  \n",
       "\n",
       "[5 rows x 206 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 训练数据/测试数据准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-15T00:56:15.971953Z",
     "start_time": "2021-03-15T00:56:15.876344Z"
    }
   },
   "outputs": [],
   "source": [
    "x_train = train.drop(['id','label'], axis=1)\n",
    "y_train = train['label']\n",
    "x_test=test.drop(['id'], axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模型训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-15T00:57:01.757175Z",
     "start_time": "2021-03-15T00:57:01.750341Z"
    }
   },
   "outputs": [],
   "source": [
    "# 评价指标\n",
    "def abs_sum(y_pre,y_tru):\n",
    "    y_pre=np.array(y_pre)\n",
    "    y_tru=np.array(y_tru)\n",
    "    loss=sum(sum(abs(y_pre-y_tru)))\n",
    "    return loss\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-15T00:57:42.940805Z",
     "start_time": "2021-03-15T00:57:42.928082Z"
    }
   },
   "outputs": [],
   "source": [
    "def cv_model(clf, train_x, train_y, test_x, clf_name):\n",
    "    folds = 5\n",
    "    seed = 2021\n",
    "    kf = KFold(n_splits=folds, shuffle=True, random_state=seed)  # 五折交叉验证\n",
    "    test = np.zeros((test_x.shape[0],4))\n",
    "\n",
    "    cv_scores = []\n",
    "    onehot_encoder = OneHotEncoder(sparse=False)  # 离散变量的编码\n",
    "    for i, (train_index, valid_index) in enumerate(kf.split(train_x, train_y)):\n",
    "        print('************************************ {} ************************************'.format(str(i+1)))\n",
    "        trn_x, trn_y, val_x, val_y = train_x.iloc[train_index], train_y[train_index], train_x.iloc[valid_index], train_y[valid_index]  # 数据集划分\n",
    "        \n",
    "        if clf_name == \"lgb\":  # lgb模型\n",
    "            train_matrix = clf.Dataset(trn_x, label=trn_y)  # 训练集\n",
    "            valid_matrix = clf.Dataset(val_x, label=val_y)  # 验证集\n",
    "\n",
    "            params = {\n",
    "                'boosting_type': 'gbdt',\n",
    "                'objective': 'multiclass',\n",
    "                'num_class': 4,\n",
    "                'num_leaves': 2 ** 5,\n",
    "                'feature_fraction': 0.8,\n",
    "                'bagging_fraction': 0.8,\n",
    "                'bagging_freq': 4,\n",
    "                'learning_rate': 0.1,\n",
    "                'seed': seed,\n",
    "                'nthread': 28,\n",
    "                'n_jobs':24,\n",
    "                'verbose': -1,\n",
    "            }\n",
    "\n",
    "            model = clf.train(params,                                               # 训练\n",
    "                      train_set=train_matrix, \n",
    "                      valid_sets=valid_matrix, \n",
    "                      num_boost_round=2000, \n",
    "                      verbose_eval=100, \n",
    "                      early_stopping_rounds=200)\n",
    "            val_pred = model.predict(val_x, num_iteration=model.best_iteration)    # 验证集预测\n",
    "            test_pred = model.predict(test_x, num_iteration=model.best_iteration)  # 测试集预测\n",
    "            \n",
    "        val_y=np.array(val_y).reshape(-1, 1)\n",
    "        val_y = onehot_encoder.fit_transform(val_y)\n",
    "        print('预测的概率矩阵为：')\n",
    "        print(test_pred)\n",
    "        test += test_pred\n",
    "        score=abs_sum(val_y, val_pred)\n",
    "        cv_scores.append(score)\n",
    "        print(cv_scores)\n",
    "    print(\"%s_scotrainre_list:\" % clf_name, cv_scores)\n",
    "    print(\"%s_score_mean:\" % clf_name, np.mean(cv_scores))\n",
    "    print(\"%s_score_std:\" % clf_name, np.std(cv_scores))\n",
    "    test=test/kf.n_splits\n",
    "\n",
    "    return test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-03-15T00:58:22.378103Z",
     "start_time": "2021-03-15T00:58:22.373222Z"
    }
   },
   "outputs": [],
   "source": [
    "def lgb_model(x_train, y_train, x_test):\n",
    "    lgb_test = cv_model(lgb, x_train, y_train, x_test, \"lgb\")\n",
    "    return lgb_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2021-03-15T00:53:32.384Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "************************************ 1 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "Training until validation scores don't improve for 200 rounds\n",
      "[100]\tvalid_0's multi_logloss: 0.0525735\n",
      "[200]\tvalid_0's multi_logloss: 0.0422444\n",
      "[300]\tvalid_0's multi_logloss: 0.0407076\n",
      "[400]\tvalid_0's multi_logloss: 0.0420398\n",
      "Early stopping, best iteration is:\n",
      "[289]\tvalid_0's multi_logloss: 0.0405457\n",
      "预测的概率矩阵为：\n",
      "[[9.99969791e-01 2.85197261e-05 1.00341946e-06 6.85357631e-07]\n",
      " [7.93287264e-05 7.69060914e-04 9.99151590e-01 2.00810971e-08]\n",
      " [5.75356884e-07 5.04051497e-08 3.15322414e-07 9.99999059e-01]\n",
      " ...\n",
      " [6.79267940e-02 4.30206297e-04 9.31640185e-01 2.81516302e-06]\n",
      " [9.99960477e-01 3.94098074e-05 8.34030725e-08 2.94638661e-08]\n",
      " [9.88705846e-01 2.14081630e-03 6.67418381e-03 2.47915423e-03]]\n",
      "[607.0736049372184]\n",
      "************************************ 2 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "Training until validation scores don't improve for 200 rounds\n",
      "[100]\tvalid_0's multi_logloss: 0.0566626\n",
      "[200]\tvalid_0's multi_logloss: 0.0450852\n",
      "[300]\tvalid_0's multi_logloss: 0.044078\n",
      "[400]\tvalid_0's multi_logloss: 0.0455546\n",
      "Early stopping, best iteration is:\n",
      "[275]\tvalid_0's multi_logloss: 0.0437793\n",
      "预测的概率矩阵为：\n",
      "[[9.99991401e-01 7.69109547e-06 6.65504756e-07 2.42084688e-07]\n",
      " [5.72380482e-05 1.32812809e-03 9.98614607e-01 2.66534396e-08]\n",
      " [2.82123411e-06 4.13195205e-07 1.34026965e-06 9.99995425e-01]\n",
      " ...\n",
      " [6.96398024e-02 6.52459907e-04 9.29685742e-01 2.19960932e-05]\n",
      " [9.99972366e-01 2.75069005e-05 7.68142933e-08 5.07415018e-08]\n",
      " [9.67263676e-01 7.26154408e-03 2.41533542e-02 1.32142531e-03]]\n",
      "[607.0736049372184, 623.4313863731124]\n",
      "************************************ 3 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "Training until validation scores don't improve for 200 rounds\n",
      "[100]\tvalid_0's multi_logloss: 0.0498722\n",
      "[200]\tvalid_0's multi_logloss: 0.038028\n",
      "[300]\tvalid_0's multi_logloss: 0.0358066\n",
      "[400]\tvalid_0's multi_logloss: 0.0361478\n",
      "[500]\tvalid_0's multi_logloss: 0.0379597\n",
      "Early stopping, best iteration is:\n",
      "[340]\tvalid_0's multi_logloss: 0.0354344\n",
      "预测的概率矩阵为：\n",
      "[[9.99972032e-01 2.62406774e-05 1.17282152e-06 5.54230651e-07]\n",
      " [1.05242811e-05 6.50215805e-05 9.99924453e-01 6.93812546e-10]\n",
      " [1.93240868e-06 1.10384984e-07 3.76773426e-07 9.99997580e-01]\n",
      " ...\n",
      " [1.34894410e-02 3.84569683e-05 9.86471555e-01 5.46564350e-07]\n",
      " [9.99987431e-01 1.25532882e-05 1.03902298e-08 5.46727770e-09]\n",
      " [9.78722948e-01 1.06329839e-02 6.94192038e-03 3.70214810e-03]]\n",
      "[607.0736049372184, 623.4313863731124, 508.0238160726953]\n",
      "************************************ 4 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "Training until validation scores don't improve for 200 rounds\n",
      "[100]\tvalid_0's multi_logloss: 0.0564768\n",
      "[200]\tvalid_0's multi_logloss: 0.0448698\n",
      "[300]\tvalid_0's multi_logloss: 0.0446719\n",
      "[400]\tvalid_0's multi_logloss: 0.0470399\n",
      "Early stopping, best iteration is:\n",
      "[250]\tvalid_0's multi_logloss: 0.0438853\n",
      "预测的概率矩阵为：\n",
      "[[9.99979692e-01 1.70821979e-05 1.27048476e-06 1.95571841e-06]\n",
      " [5.66207785e-05 4.02275314e-04 9.99541086e-01 1.82828519e-08]\n",
      " [2.62267451e-06 3.58613522e-07 4.78645006e-06 9.99992232e-01]\n",
      " ...\n",
      " [4.56636552e-02 5.69497433e-04 9.53758468e-01 8.37980573e-06]\n",
      " [9.99896785e-01 1.02796802e-04 2.46636563e-07 1.72061021e-07]\n",
      " [8.70911669e-01 1.73790185e-02 1.04478175e-01 7.23113697e-03]]\n",
      "[607.0736049372184, 623.4313863731124, 508.0238160726953, 660.4867407547267]\n",
      "************************************ 5 ************************************\n",
      "[LightGBM] [Warning] num_threads is set with n_jobs=24, nthread=28 will be ignored. Current value: num_threads=24\n",
      "Training until validation scores don't improve for 200 rounds\n",
      "[100]\tvalid_0's multi_logloss: 0.0506398\n",
      "[200]\tvalid_0's multi_logloss: 0.0396422\n",
      "[300]\tvalid_0's multi_logloss: 0.0381065\n",
      "[400]\tvalid_0's multi_logloss: 0.0390162\n",
      "[500]\tvalid_0's multi_logloss: 0.0414986\n",
      "Early stopping, best iteration is:\n",
      "[324]\tvalid_0's multi_logloss: 0.0379497\n",
      "预测的概率矩阵为：\n",
      "[[9.99993352e-01 6.02902202e-06 1.13002685e-07 5.06277302e-07]\n",
      " [1.03959552e-05 5.03778956e-04 9.99485820e-01 5.07638601e-09]\n",
      " [1.92568065e-07 5.07155306e-08 4.94690856e-08 9.99999707e-01]\n",
      " ...\n",
      " [8.83103121e-03 2.51969353e-05 9.91142776e-01 9.96143937e-07]\n",
      " [9.99984791e-01 1.51997858e-05 5.62426491e-09 3.80450197e-09]\n",
      " [9.86084001e-01 8.75968498e-04 1.09742304e-02 2.06580027e-03]]\n",
      "[607.0736049372184, 623.4313863731124, 508.0238160726953, 660.4867407547267, 539.2160054696063]\n",
      "lgb_scotrainre_list: [607.0736049372184, 623.4313863731124, 508.0238160726953, 660.4867407547267, 539.2160054696063]\n",
      "lgb_score_mean: 587.6463107214719\n",
      "lgb_score_std: 55.94453640571462\n"
     ]
    }
   ],
   "source": [
    "lgb_test = lgb_model(x_train, y_train, x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2021-03-15T00:53:33.065Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9.99981254e-01, 1.71125438e-05, 8.45046636e-07, 7.88733736e-07],\n",
       "       [4.28215579e-05, 6.13652971e-04, 9.99343511e-01, 1.41575174e-08],\n",
       "       [1.62884845e-06, 1.96662878e-07, 1.37365693e-06, 9.99996801e-01],\n",
       "       ...,\n",
       "       [4.11101448e-02, 3.43163508e-04, 9.58539745e-01, 6.94675406e-06],\n",
       "       [9.99960370e-01, 3.94933168e-05, 8.45736848e-08, 5.23076338e-08],\n",
       "       [9.58337628e-01, 7.65806626e-03, 3.06443728e-02, 3.35993298e-03]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lgb_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2021-03-15T00:53:33.810Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.999981</td>\n",
       "      <td>1.711254e-05</td>\n",
       "      <td>8.450466e-07</td>\n",
       "      <td>7.887337e-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000043</td>\n",
       "      <td>6.136530e-04</td>\n",
       "      <td>9.993435e-01</td>\n",
       "      <td>1.415752e-08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.000002</td>\n",
       "      <td>1.966629e-07</td>\n",
       "      <td>1.373657e-06</td>\n",
       "      <td>9.999968e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.999970</td>\n",
       "      <td>1.909713e-05</td>\n",
       "      <td>1.097002e-05</td>\n",
       "      <td>3.576703e-08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.999983</td>\n",
       "      <td>1.769712e-06</td>\n",
       "      <td>1.482817e-05</td>\n",
       "      <td>1.966254e-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19995</th>\n",
       "      <td>0.998096</td>\n",
       "      <td>3.060176e-04</td>\n",
       "      <td>1.085313e-04</td>\n",
       "      <td>1.489757e-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19996</th>\n",
       "      <td>0.999846</td>\n",
       "      <td>1.436305e-04</td>\n",
       "      <td>1.074898e-05</td>\n",
       "      <td>8.837766e-08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19997</th>\n",
       "      <td>0.041110</td>\n",
       "      <td>3.431635e-04</td>\n",
       "      <td>9.585397e-01</td>\n",
       "      <td>6.946754e-06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19998</th>\n",
       "      <td>0.999960</td>\n",
       "      <td>3.949332e-05</td>\n",
       "      <td>8.457368e-08</td>\n",
       "      <td>5.230763e-08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19999</th>\n",
       "      <td>0.958338</td>\n",
       "      <td>7.658066e-03</td>\n",
       "      <td>3.064437e-02</td>\n",
       "      <td>3.359933e-03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>20000 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              0             1             2             3\n",
       "0      0.999981  1.711254e-05  8.450466e-07  7.887337e-07\n",
       "1      0.000043  6.136530e-04  9.993435e-01  1.415752e-08\n",
       "2      0.000002  1.966629e-07  1.373657e-06  9.999968e-01\n",
       "3      0.999970  1.909713e-05  1.097002e-05  3.576703e-08\n",
       "4      0.999983  1.769712e-06  1.482817e-05  1.966254e-07\n",
       "...         ...           ...           ...           ...\n",
       "19995  0.998096  3.060176e-04  1.085313e-04  1.489757e-03\n",
       "19996  0.999846  1.436305e-04  1.074898e-05  8.837766e-08\n",
       "19997  0.041110  3.431635e-04  9.585397e-01  6.946754e-06\n",
       "19998  0.999960  3.949332e-05  8.457368e-08  5.230763e-08\n",
       "19999  0.958338  7.658066e-03  3.064437e-02  3.359933e-03\n",
       "\n",
       "[20000 rows x 4 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp=pd.DataFrame(lgb_test)\n",
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2021-03-15T00:53:34.680Z"
    }
   },
   "outputs": [],
   "source": [
    "result=pd.read_csv('./data/sample_submit.csv')\n",
    "result['label_0']=temp[0]\n",
    "result['label_1']=temp[1]\n",
    "result['label_2']=temp[2]\n",
    "result['label_3']=temp[3]\n",
    "result.to_csv('./data/submit.csv',index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "线上得分：559.61"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
